var e = getApp();

e.util.url("entry/wxapp/Api", {
    m: "monai_market",
    uid: wx.getStorageSync("uid"),
    r: "Upimg"
});

Page({
    data: {
        series: '',
        vip: 0,
        js: 0,
        upcarindex: 0,
        date: "2017-01",
        carsimgs: [{
            img: "",
            intro: "",
            imgshort: ""
        }],
        cmpimgs: [{
            img: "",
            intro: "",
            imgshort: ""
        }],
        brandid: "",
        brandname: "",
        classid: "",
        classes: [],
        brands: [],
        ppname: '', //汽车品牌
        location: "请选择看车地址",
        selectclass: "请选择分类",
        selectbrand: "请选择车辆款型",
        selecttime: " 请选择上牌日期",
        selecttime1: "",
        selecttimes: " 请选择出厂日期",
        selecttimes1: "",
        vehicletime: "请选择上牌日期",
        vehicletime1: "",
        insurance: "请选择保险到期时间",
        insurance1: "",
        gearboxes: [{
            name: "手自一体",
            id: "1"
        }, {
            name: "手动档",
            id: "2"
        }, {
            name: "自动挡",
            id: "3"
        }],
        gearboxname: "请选择变速箱类型",
        gearboxindex: 0,
        userphone: "",
        mapname: "",
        mapx: "",
        mapy: "",
        carimgs: [],
        buttscan: !1,
        notice: [],
        noticeindex: 0,
        textareatype: !1,
        textareaname: "请输入车辆信息介绍",
        textareainput: "",
        isShow: !1,
        showtextarea: !1,
        status: "",
        scrollleft: 99999
    },
    intextarea: function() {
        this.setData({
            textareatype: !0,
            textareaname: this.data.textareainput
        });
    },

    bindblur: function(e) {
        console.log(e.detail.value);
        var t = e.detail.value,
            a = t;
        t || (t = "请输入车辆信息介绍", a = ""), this.setData({
            textareatype: !1,
            textareaname: t,
            textareainput: a
        });
    },
    js: function(cc) {
        console.log(cc)
        var c = cc.detail.value
        if (c == false) {
            this.setData({
                js: 0
            })
        } else {
            this.setData({
                js: 1
            })
        }

    },
    qs: function() {
        wx.navigateTo({
            url: '../qs/qs?qq=1',
        })
    },
    bindconfirm: function() {
        var e = new Date();
        console.log(e.getMilliseconds());
    },
    hideDialog: function() {
        this.setData({
            isShow: !this.data.isShow
        });
    },
    updateUserInfo: function(t) {
        var a = this;
        e.util.getUserInfo(function(e) {
            wx.setStorageSync("uid", e.memberInfo.uid), a.hideDialog();
        }, t.detail);
    },
    showtextarea: function() {
        var e = this;
        console.log(!e.data.showtextarea), e.setData({
            showtextarea: !e.data.showtextarea
        });
    },
    textareashow: function(e) {
        this.setData({
            mapname: e.detail.value
        });
    },
    // dldl:function(e){
    //   wx.navigateTo({
    //     url: '../dl/dl',
    //   })
    // },
    onShow: function(tt) {
        var g = this;
        if (e.globalData.phone != '') {
            console.log('jinlai')
            g.setData({
                userphone: e.globalData.phone
            })
        }
        setTimeout(function() {
            if (g.data.userphone == '') {
                wx.showModal({
                    content: "请先创建店铺",
                    cancelText: "返回",
                    confirmText: "去创建",
                    success: function(e) {
                        console.log(e)
                        if (e.confirm == true) {
                            wx.navigateTo({
                                url: '../dl/dl',
                            })
                        }
                    }
                })
            }
        }, 1000)
        if (e.globalData.id != '' && e.globalData.name != '') {
            g.setData({
                selectbrand: e.globalData.name,
                brandid: e.globalData.ppid,
                ppname: e.globalData.ppname,
                series: e.globalData.series,
            });
        }

        "" != g.data.brandid && "" != g.data.brandname && g.setData({
            selectbrand: g.data.brandname
        });
        console.log('data', g.data)
    },
    firstin: function(t) {
        var a = wx.getSystemInfoSync();
        t.setData({
            maxwidth: a.windowWidth,
            maxheight: a.windowHeight
        }), e.util.request({
            url: "entry/wxapp/Api",
            data: {
                m: "monai_market",
                uid: e.getuid(),
                r: "sale.index.saleindex"
            },
            cachetime: "0",
            success: function(e) {
                console.log('leix', e)
                var a = {
                    classes: e.data.data.class,
                    selectclass: e.data.data.class[0].name,
                    classid: e.data.data.class[0].id,
                    userphone: e.data.data.user.phone,
                    mapname: ""
                };
                e.data.data.user && (a.userphone = e.data.data.user.phone, a.mapname = e.data.data.user.address),
                    t.setData(a);
            }
        });
        var that = this
        setTimeout(function() {
            console.log('sssssssssssss', that.data.userphone)
            if (that.data.userphone == '') {
                wx.showModal({
                    content: "请先创建店铺",
                    cancelText: "返回",
                    confirmText: "去创建",
                    success: function(e) {
                        console.log(e)
                        if (e.confirm == true) {
                            wx.navigateTo({
                                url: '../dl/dl',
                            })
                        }
                    }
                })
            }
        }, 1000)


    },
    onLoad: function(t) {
        var a = this;
        console.log('index', a.data)
        console.log('tttt', t)
        if (t.phone != null) {
            console.log('jinlai')
            a.setData({
                userphone: t.phone
            })
        }
        e.util.getUserInfo(function(t) {
            t.memberInfo || a.setData({
                isShow: !0
            }), e.util.request({
                url: "entry/wxapp/Api",
                data: {
                    m: "monai_market",
                    r: "me.info",
                    uid: wx.getStorageSync("uid")
                },
                success: function(ee) {
                    e.globalData.vip = ee.data.data.is_vip
                    console.log(ee), 1 == ee.data.data.status && wx.showModal({
                        title: "提示",
                        content: "您的用户已经禁止发布车辆信息，请联系管理员处理",
                        showCancel: !1,
                        success: function(e) {}
                    }), a.setData({
                        vip: ee.data.data.is_vip,
                        status: ee.data.data.status
                    });
                }
            });
        }), a.firstin(a);
    },
    selectclass: function(e) {
        var t = this,
            a = t.data.classes[e.detail.value];
        console.log(a)
        a && t.setData({
            selectclass: a.name,
            classid: a.id
        });
    },
    gearbox: function(e) {
        var t = this,
            a = t.data.gearboxes[e.detail.value];
        a && t.setData({
            gearboxname: a.name,
            gearboxindex: e.detail.value
        });
    },
    selectbrand: function(e) {
        wx.navigateTo({
            url: "/pages/selectscards/index?xiugai=2"
        });
    },
    bindDateChange: function(e) {
        console.log(e.detail.value);
        var t = e.detail.value.split("-");
        this.setData({
            selecttime: t[0] + "年" + t[1] + "月",
            selecttime1: e.detail.value
        });
    },
    bindDateChange11: function(e) {
        console.log(e.detail.value);
        var t = e.detail.value.split("-");
        this.setData({
            selecttimes: t[0] + "年" + t[1] + "月",
            selecttimes1: e.detail.value
        });
    },
    insurance: function(e) {
        console.log(e.detail.value);
        var t = e.detail.value.split("-");
        this.setData({
            insurance: t[0] + "年" + t[1] + "月" + t[2] + "日",
            insurance1: e.detail.value
        });
    },
    vehicletime: function(e) {
        console.log(e.detail.value);
        var t = e.detail.value.split("-");
        this.setData({
            vehicletime: t[0] + "年" + t[1] + "月" + t[2] + "日",
            vehicletime1: e.detail.value
        });
    },
    getPhoneNumber: function(t) {
        console.log(t);
        var a = this;
        e.util.request({
            url: "entry/wxapp/Api",
            data: {
                r: "sale.index.userphone",
                m: "monai_market",
                iv: t.detail.iv,
                encryptedData: t.detail.encryptedData
            },
            cachetime: "0",
            success: function(e) {
                a.setData({
                    userphone: e.data.data
                });
            }
        });
    },
    tomap: function() {
        var e = this;
        wx.chooseLocation({
            success: function(t) {
                console.log(t), e.setData({
                    mapx: t.latitude,
                    mapy: t.longitude,
                    mapname: t.address
                });
            },
            fail: function() {
                wx.getSetting({
                    success: function(e) {
                        e.authSetting["scope.userLocation"] || wx.showModal({
                            content: "请允许获取地理位置后再次尝试",
                            success: function(e) {
                                e.confirm ? wx.openSetting({
                                    success: function(e) {
                                        e.authSetting = {
                                            "scope.userInfo": !0,
                                            "scope.userLocation": !0
                                        };
                                    },
                                    fail: function(e) {
                                        console.log(e);
                                    }
                                }) : e.cancel && console.log("用户点击取消");
                            }
                        });
                    }
                });
            }
        });
    },
    release: function(t) {
        console.log(t)
        var a = this,
            i = wx.getStorageSync("uid");
        if (a.data.userphone == '') {
            wx.showModal({
                content: "请先创建店铺",
                cancelText: "返回",
                confirmText: "去创建",
                success: function(e) {
                    console.log(e)
                    if (e.confirm == true) {
                        wx.navigateTo({
                            url: '../dl/dl',
                        })
                    }
                }
            })
        } else {
            if (console.log(t.detail.value.address), i) {
                a.setData({
                    buttscan: !0
                });
                var s = a.data.classid;
                if ("" == s || "undefined" == s || "请选择车辆类型" == s) return e.util.message({
                    title: "请选择车辆类型",
                    type: "error"
                }), void a.setData({
                    buttscan: !1
                });
                var n = a.data.brandid;
                if ("" == n || "undefined" == n || "请选择车辆品牌" == n) return e.util.message({
                    title: "请选择车辆品牌",
                    type: "error"
                }), void a.setData({
                    buttscan: !1
                });
                if (a.data.vip == 3) {
                    var r = ''
                } else {
                    var r = a.data.selecttime;
                    if ("请选择上牌日期" == r || "undefined" == r || "" == a.data.selecttime1) return e.util.message({
                        title: "请选择上牌日期",
                        type: "error"
                    }), void a.setData({
                        buttscan: !1
                    });
                }
                var chuchang = a.data.selecttimes;
                if ("请选择出厂日期" == r || "undefined" == r || "" == a.data.selecttimes1) return e.util.message({
                    title: "请选择出厂日期",
                    type: "error"
                }), void a.setData({
                    buttscan: !1
                });
                var o = a.data.selectbrand;
                if ("" == o || "undefined" == o) return e.util.message({
                    title: "请选择车辆款型",
                    type: "error"
                }), void a.setData({
                    buttscan: !1
                });
                // var o = t.detail.value.carname;
                // if ("" == o || "undefined" == o) return e.util.message({
                //     title: "请填写汽车名称",
                //     type: "error"
                // }), void a.setData({
                //     buttscan: !1
                // });
                if (a.data.vip == 3) {
                    var u = ''
                } else {
                    var u = t.detail.value.kilometre;
                    if ("" == u || "undefined" == u) return e.util.message({
                        title: "请填写实际里程",
                        type: "error"
                    }), void a.setData({
                        buttscan: !1
                    });
                }
                if (a.data.vip == 3) {
                    var c = ''
                } else {
                    var c = t.detail.value.price;
                    if ("" == c || "undefined" == c) return e.util.message({
                        title: "请填写售价",
                        type: "error"
                    }), void a.setData({
                        buttscan: !1
                    });
                }
                if (a.data.vip == 3) {
                    var ghcs = ''
                } else {
                    var ghcs = t.detail.value.transfer;
                    if ("" == c || "undefined" == c) return e.util.message({
                        title: "请填写过户次数",
                        type: "error"
                    }), void a.setData({
                        buttscan: !1
                    });
                }


                if (a.data.vip == 2) {
                    var dc = ''
                } else {
                    var dc = t.detail.value.lowprice;
                    if ("" == dc || "undefined" == dc) return e.util.message({
                        title: "请填写批发价",
                        type: "error"
                    }), void a.setData({
                        buttscan: !1
                    });
                }

                var d = t.detail.value.exhaust;
                if ("" == d || "undefined" == d) return e.util.message({
                    title: "请填写排气量",
                    type: "error"
                }), void a.setData({
                    buttscan: !1
                });
                if (a.data.vip == 3) {
                    var nn = ''
                } else {
                    var nn = t.detail.value.username;
                    if ("" == nn || "undefined" == nn) return e.util.message({
                        title: "请填写联系人",
                        type: "error"
                    }), void a.setData({
                        buttscan: !1
                    });
                }
                var l = a.data.gearboxindex;
                if ("" == l || "undefined" == l) return e.util.message({
                    title: "请选择变速箱类型",
                    type: "error"
                }), void a.setData({
                    buttscan: !1
                });
                if (a.data.vip == 3) {
                    var kcdz = ''
                } else {
                    var kcdz = a.data.location;
                    if ("" == kcdz || "undefined" == kcdz || "请选择看车地址" == kcdz) return e.util.message({
                        title: "请选择看车地址",
                        type: "error"
                    }), void a.setData({
                        buttscan: !1
                    });
                }
                if (a.data.vip == 3) {
                    var m = ''
                } else {
                    var m = t.detail.value.phone;
                    if ("" == m || "undefined" == m) return e.util.message({
                        title: "请填写联系电话，或点击电话授权",
                        type: "error"
                    }), void a.setData({
                        buttscan: !1
                    });
                }
                if (a.data.vip == 3) {
                    var g = ''
                } else {
                    var g = t.detail.value.address;
                    if ("" == g || "undefined" == g) return e.util.message({
                        title: "请填写车辆地址",
                        type: "error"
                    }), void a.setData({
                        buttscan: !1
                    });
                }
                if (a.data.vip == 3) {
                    var sy = ''
                } else {
                    var sy = t.detail.value.synopsis;
                    console.log('ttttt', t)
                    if ("" == sy || "undefined" == sy) return e.util.message({
                        title: "请填写车况描述",
                        type: "error"
                    }), void a.setData({
                        buttscan: !1
                    });
                }
                var p = a.data.carsimgs;
                if (p.length < 6) return e.util.message({
                    title: "请至少添加6个车辆图文",
                    type: "error"
                }), void a.setData({
                    buttscan: !1
                });
                if (a.data.vip == 3) {
                    var cs = ''
                } else {
                    var cs = a.data.cmpimgs;
                    if (cs.length < 1) return e.util.message({
                        title: "请至少添加1个铭牌图文",
                        type: "error"
                    }), void a.setData({
                        buttscan: !1
                    });
                    var nm = [];
                    for (var f in cs) "" != cs[f].imgshort && "" != cs[f].img && (console.log(cs[f]), console.log(cs[f].imgshort),
                        nm.push(cs[f]));
                    var dd = nm[0].imgshort
                    console.log(dd)
                }
                var h = [];
                for (var f in p) "" != p[f].imgshort && "" != p[f].img && (console.log(p[f]), console.log(p[f].imgshort),
                    h.push(p[f]));


                console.log(nm)

                var v = {
                    r: "sale.index.insale",
                    m: "monai_market",
                    fomid: t.detail.formId,
                    uid: i,
                    classid: s,
                    brand: n,
                    exhaust: d,
                    times: r,
                    times1: a.data.selecttime1,
                    factory_time: chuchang,
                    kilometre: u,
                    price: c,
                    low_price: dc,
                    gearboxindex: l,
                    phone: m,
                    carname: o,
                    address: g,
                    carimgs: h,
                    name_plate: dd,
                    username: nn,
                    mapx: a.data.mapx,
                    mapy: a.data.mapy,
                    synopsis: sy,
                    urgency: a.data.js,
                    series: a.data.series,
                    location: kcdz,
                    transfer: ghcs
                };
                console.log(v)
                e.util.request({
                    url: "entry/wxapp/Api",
                    data: v,
                    cachetime: "0",
                    success: function(e) {
                        a.setData({
                            selectbrand: '请选择名称',
                            brandid: '',
                            ppname: '',
                            cmpimgs: [{
                                img: "",
                                intro: "",
                                imgshort: ""
                            }],
                        })
                        e && e.data && 405 == e.data.message ? a.setData({
                            isShow: !0,
                            buttscan: !1
                        }) : e && e.data && e.data.data && wx.showModal({
                            content: "提交成功，快去上架吧",
                            cancelText: "继续添加",
                            confirmText: "去上架",
                            success: function(e) {
                                e.confirm ? (a.cleardetail(), wx.navigateTo({
                                    url: "/pages/salelist/index"
                                })) : e.cancel && (a.setData({
                                    buttscan: !1
                                }), wx.switchTab({
                                    url: "/pages/sale/index"
                                }));
                            }
                        });


                    },
                    fail: function(e) {
                        a.setData({
                            buttscan: !1
                        });
                    }
                });
            } else a.setData({
                isShow: !0
            });

        }



    },
    cleardetail: function() {
        this.setData({
            selectclass: "请选择分类",
            selectbrand: "请选择名称",
            selecttime: " 请选择上牌日期",
            selecttime1: "",
            selecttimes: " 请选择出厂日期",
            selecttimes1: "",
            vehicletime1: "",
            insurance1: "",
            gearboxname: "请选择变速箱类型",
            gearboxindex: 0,
            mapx: "",
            mapy: "",
            carimgs: [],
            textareaname: "请输入车辆信息介绍",
            textareainput: "",
            carsimgs: [{
                img: "",
                intro: "",
                imgshort: ""
            }],
            brandid: "",
            brandname: "",
            classid: "",
            defult_value: ""
        });
    },
    upnotice: function(e) {
        this.data.noticeindex = e.detail.current;
    },
    retonotice: function() {
        var e = this.data.notice[this.data.noticeindex];
        e && e.id && wx.navigateTo({
            url: "/pages/index/index?id=" + e.id
        });
    },
    delcarsimgs: function(t) {
        var a = this,
            i = t.currentTarget.dataset.index,
            s = a.data.carsimgs;
        if (0 == i && s.length > 1 && ("" != s[i].img || "" != s[i].intro)) return "" != s[i].img && e.util.request({
            url: "entry/wxapp/Api",
            data: {
                m: "monai_market",
                uid: e.getuid(),
                imgurl: s[i].img,
                r: "Upimg.delImg"
            }
        }), s.splice(i, 1), void a.setData({
            carsimgs: s
        });
        0 != i && ("" != s[i].img && e.util.request({
                url: "entry/wxapp/Api",
                data: {
                    m: "monai_market",
                    uid: e.getuid(),
                    imgurl: s[i].img,
                    r: "Upimg.delImg"
                }
            }),
            s.splice(i, 1),
            a.setData({
                carsimgs: s
            }));
    },
    upcarsimgs: function(t) {
        var a = this;
        a.data.carsimgs.length >= 16 ? e.util.message({
            title: "您添加的图片已经很多了",
            type: "error"
        }) : e.upimgs({
            scannums: 16 - a.data.carsimgs.length,
            filename: "cars",
            success: function(e) {
                for (var t in e) a.addinimgs(e[t]);
                console.log(e)
                console.log(a.data.carsimgs)
                a.setData({
                    scrollleft: 99999
                })
            }
        });
    },
    upcarsimgs2: function(t) {
        var a = this;
        a.data.carsimgs.length >= 9 ? e.util.message({
            title: "您添加的图片已经很多了",
            type: "error"
        }) : e.upimgs({
            scannums: 1,
            filename: "cars",
            success: function(e) {
                for (var t in e) a.addinimgs(e[t]);
                console.log(e)
                console.log(a.data.carsimgs)
            }
        });
    },
    addinimgs: function(e) {
        var t = this,
            a = t.data.carsimgs,
            i = {
                img: e.all,
                imgshort: e.short
            };
        a.push(i), t.setData({
            carsimgs: a
        });
    },
    showimgs: function(e) {
        var t = this,
            a = e.currentTarget.dataset.index,
            i = t.data.carsimgs;
        wx.previewImage({
            current: i[a].img,
            urls: [i[a].img]
        });
    },
    upthiscarsimgs: function(t) {
        console.log(t)
        console.log('1')
        var a = this,
            i = t.currentTarget.dataset.index,
            s = a.data.carsimgs;
        e.util.request({
                url: "entry/wxapp/Api",
                data: {
                    m: "monai_market",
                    uid: e.getuid(),
                    imgurl: s[i].img,
                    r: "Upimg.delImg"
                }
            }), s[i].img = "",
            s.splice(i, 1);
        console.log(t)
        a.setData({
            carsimgs: s
        });
    },
    upcarsimgs1: function(t) {
        var a = this;
        a.data.cmpimgs.length >= 9 ? e.util.message({
            title: "您添加的图片已经很多了",
            type: "error"
        }) : e.upimgs({
            scannums: 1,
            filename: "cars",
            success: function(e) {
                for (var t in e) a.addinimgs1(e[t]);
                console.log(e)
                console.log(a.data.cmpimgs)
            }
        });
    },
    upthiscarsimgs1: function(t) {
        console.log(t)
        console.log('1')
        var a = this,
            i = t.currentTarget.dataset.index,
            s = a.data.cmpimgs;
        e.util.request({
                url: "entry/wxapp/Api",
                data: {
                    m: "monai_market",
                    uid: e.getuid(),
                    imgurl: s[i].img,
                    r: "Upimg.delImg"
                }
            }), s[i].img = "",
            s.splice(i, 1);
        console.log(t)
        a.setData({
            cmpimgs: s
        });
    },
    showimgs1: function(e) {
        var t = this,
            a = e.currentTarget.dataset.index,
            i = t.data.cmpimgs;
        wx.previewImage({
            current: i[a].img,
            urls: [i[a].img]
        });
    },
    addinimgs1: function(e) {
        var t = this,
            a = t.data.cmpimgs,
            i = {
                img: e.all,
                imgshort: e.short
            };
        a.push(i), t.setData({
            cmpimgs: a
        });
    },
    setthisintro: function(e) {
        var t = this,
            a = e.currentTarget.dataset.index;
        t.data.carsimgs[a].intro = e.detail.value;
    },
    addcarsimgs: function() {
        var e = {
                img: "",
                intro: "",
                imgshort: ""
            },
            t = this.data.carsimgs;
        t.push(e), this.setData({
            carsimgs: t
        });
    },
    upimg: function() {
        var t = this,
            a = t.data.carimgs;
        if (a.length >= 9) e.util.message({
            title: "您上传的图片已经很多了",
            type: "error"
        });
        else {
            a.length;
            e.upimgs({
                scannums: 1,
                filename: "cars",
                success: function(e) {
                    var i = a.concat(e);
                    t.setData({
                        carimgs: i
                    });
                    console.log("t.data", t.data);
                }
            });
        }
    },
    delimg: function(t) {
        console.log(t.currentTarget.dataset.index);
        var a = this,
            i = t.currentTarget.dataset.index,
            s = a.data.carimgs,
            n = s[i].all;
        e.util.request({
            url: "entry/wxapp/Api",
            data: {
                m: "monai_market",
                uid: e.getuid(),
                imgurl: n,
                r: "Upimg.delImg"
            }
        }), s.splice(i, 1), a.setData({
            carimgs: s
        });
    },
    onReady: function() {},
    onHide: function() {},
    onUnload: function() {},
    onPullDownRefresh: function() {},
    onReachBottom: function() {},
    onShareAppMessage: function() {}
});